<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          :disabled="scene === 0 ? false : true"
          value-key=""
          placeholder="请选择"
          v-model="categoryStore.c1Id"
          clearable
          filterable
          @change="handler"
          style="width: 120px"
        >
          <!-- label即为显示的文字，value属性即为select下拉收集的数据 -->
          <el-option
            :label="c1.name"
            filterable
            clearable
            v-for="c1 in categoryStore.c1Arr"
            :key="c1.id"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene === 0 ? false : true"
          value-key=""
          placeholder="请选择"
          @change="handler1"
          v-model="categoryStore.c2Id"
          style="width: 120px"
        >
          <el-option
            :label="c2.name"
            v-for="c2 in categoryStore.c2Arr"
            :key="c2.id"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene === 0 ? false : true"
          value-key=""
          placeholder="请选择"
          v-model="categoryStore.c3Id"
          style="width: 120px"
        >
          <el-option
            :label="c3.name"
            v-for="c3 in categoryStore.c3Arr"
            :key="c3.id"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
//引入生命周期钩子
import { onMounted, defineProps } from 'vue'

//引入分类相关的仓库
import useCategoryStore from '@/store/modules/category'
//使用仓库
let categoryStore = useCategoryStore()

//接收父组件传入的scene值
defineProps(['scene'])

//二级分类下拉change事件，选中值获取触发三级分类数据
const handler1 = () => {
  //清理三级分类的数据
  categoryStore.c3Id = ''
  //通知仓库获取三级分类的数据
  categoryStore.getC3()
}

//一级分类的下拉change事件,选中值的时候会触发,保证1级分类选择
const handler = () => {
  //清空二级、三级数据
  categoryStore.c2Id = ''
  categoryStore.c3Id = ''
  categoryStore.c3Arr = []
  //通知仓库获取二级分类的数据
  categoryStore.getC2()
}

//从仓库获取分类的方法
const getC1 = () => {
  //组件挂载完毕获取一级分类数据
  categoryStore.getC1()
}

//组件挂载完毕
onMounted(() => {
  //获取一级分类的数据
  getC1()
})
</script>

<style lang="scss" scoped></style>
